package com.smart.config.satoken;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handle -> {
                    String uri = SaHolder.getRequest().getRequestPath();
                    // 放行 MagicAPI 控制台 WebSocket
                    if (uri.startsWith("/magic/web/console")) {
                        return;
                    }
                    StpUtil.checkLogin();
                }))
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/auth/login",
                        "/auth/logout",
                        "/auth/verify",
                        "/swagger-ui/**",
                        "/webjars/**",
                        "/v3/api-docs/**",
                        "/swagger-resources",
                        "/localFile/**",
                        "/magic/web/**",
                        "/magic/websocket/**",
                        "/magic/web/console/**",
                        "/magic/**/console",
                        "/magic/**/login",
                        "/magic/**/logout",
                        "/magic/**/config.json"
                );
    }
}
